package cn.hxzy.service.impl;

import cn.hxzy.domain.Chapter;
import cn.hxzy.domain.ClassMenu;
import cn.hxzy.domain.Course;
import cn.hxzy.domain.Section;
import cn.hxzy.mapper.ChapterMapper;
import cn.hxzy.mapper.CourseMapper;
import cn.hxzy.mapper.SectionMapper;
import cn.hxzy.response.R;
import cn.hxzy.service.CourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collections;
import java.util.List;

/**
 * @Author LiDong
 * @Description
 * @create 2024/6/22 16:48
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private ChapterMapper chapterMapper;

    @Autowired
    private SectionMapper sectionMapper;

    @Override
    public R createCourse(Course course) {
        int insert = courseMapper.insert(course);
        if (insert != 1) {
            return R.error(400, "创建失败");
        }
        return R.success(null);
    }

    @Override
    public R updateCourse(Course courseDetails) {
        int i = courseMapper.updateById(courseDetails);
        if (i != 1) {
            return R.error(400, "修改失败");
        }
        return R.success(null);
    }

    @Override
    public Boolean deleteCourse(Long id) {
        int i = courseMapper.deleteById(id);
        if (i != 1) {
            return false;
        }
        return true;
    }

    @Override
    public R createChapter(Chapter chapter) {
        int i = chapterMapper.insert(chapter);
        if (i > 0) {
            return R.success(chapter);
        }
        return R.error(400, "添加失败");
    }

    @Override
    public R createSection(Section section) {
        int result = sectionMapper.insert(section);
        if (result > 0) {
            return R.success(section);
        }
        return R.error(400, "添加失败");
    }

    @Override
    public void uploadMaterial(Long sectionId, MultipartFile file) {

    }

    @Override
    public List<Course> searchCourse(String searchName) {
        return courseMapper.searchCourse(searchName);
    }

    @Override
    public List<Course> listCourse() {
        return null;
    }

    @Override
    public R<List<Chapter>> listChapter(Long courseId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("course_id", courseId);
        List list = courseMapper.selectList(queryWrapper);
        return R.success(list);
    }

    @Override
    public List<Section> listSection(Long chapterId) {
        QueryWrapper<Section> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chapter_id", chapterId);
        return sectionMapper.selectList(queryWrapper);
    }

    @Override
    public Integer grantClass(Long courseId, List<String> classIds) {
        return null;
    }

    @Override
    public Integer grantStudent(Long courseId, List<String> studentIds) {
        return null;
    }

    /**
     * 给班级赋权
     *
     * @param courseId 课程id
     * @param classIdList  班级id
     * @return 影响行数
     */
    @Override
    public int addGrantClass(Long courseId, List<Integer> classIdList) {
        classIdList.forEach(classId -> {
            ClassMenu  classMenu  = new ClassMenu();
            classMenu.setCourseId(courseId.intValue());
            classMenu.setClassId(classId);
            int i = courseMapper.addGrantClass(classMenu);
            if (i != 1) {
                throw new RuntimeException("添加失败");
            }

        });
        return 1;
    }

    @Override
    public List<Integer> getCourseIdByClassId(Long classId) {
        List<Integer> courseIdByClassId = courseMapper.getCourseIdByClassId(classId);
        return courseIdByClassId;
    }


    public List<Course> listCourse(Long seationId) {
        return null;
    }
}
